//
//  UserSettingViewController.m
//  Ridding
//
//  Created by zys on 12-5-2.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "UserSettingViewController.h"
#import "UMFeedback.h"
#import "TutorialViewController.h"
#import "UIColor+XMin.h"
@implementation UserSettingViewController
@synthesize uiTableView;
@synthesize staticInfo;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        staticInfo=[StaticInfo getSinglton];
    }
    return self;
}

- (void)viewDidLoad
{
    uiTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
    self.uiTableView.delegate=self;
    self.uiTableView.dataSource=self;
    [uiTableView reloadData];
    [self.view addSubview:uiTableView];
    [super viewDidLoad];
    
    //设置导航栏背景
    CGSize size = CGSizeMake(320, 44);
    UIImage *image = [self OriginImage:[UIImage imageNamed:@"navbg.png"] scaleToSize:size];
    [self.navigationController.navigationBar setBackgroundImage:image
                                                  forBarMetrics:UIBarMetricsDefault];
    //设置导航栏按钮    
    UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleBordered target:self action:@selector(back)];
    [customBarItem setBackgroundImage:[UIImage imageNamed:@"返回按钮1.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    [customBarItem setBackgroundImage:[UIImage imageNamed:@"返回按钮2.png"] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
    self.navigationItem.leftBarButtonItem = customBarItem;
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont boldSystemFontOfSize:20.0];
    label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
    label.textAlignment = UITextAlignmentCenter;
    label.textColor = [UIColor whiteColor]; // change this color
    self.navigationItem.titleView = label;
    label.text = @"设置";
    [label sizeToFit];
    
    [uiTableView setBackgroundColor:[UIColor getColor:@"E6E6E6"]];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = NO;
    [super viewWillAppear:animated];
    
}
- (void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;
   	[super viewWillDisappear:animated];
}

-(void)back {  
    // Tell the controller to go back
    [self.navigationController popViewControllerAnimated:YES];
}




-(UIImage*)  OriginImage:(UIImage *)image scaleToSize:(CGSize)size    
{    
    // 创建一个bitmap的context    
    // 并把它设置成为当前正在使用的context    
    UIGraphicsBeginImageContext(size);    
    
    // 绘制改变大小的图片    
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];    
    
    // 从当前context中创建一个改变大小后的图片    
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();    
    
    // 使当前的context出堆栈    
    UIGraphicsEndImageContext();    
    
    // 返回新的改变大小后的图片    
    return scaledImage;    
} 



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait||interfaceOrientation ==UIInterfaceOrientationPortraitUpsideDown);
}
#pragma mark -
#pragma mark UITableView data source and delegate methods
//每个section显示的标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return @"";
}

//指定有多少个分区(Section)，默认为1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if(section == 0){
        return 4;//推荐、帮助、升级
    }
    if(section == 1){
        return 1;//退出,注销
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{	
    static NSString *kCellID = @"CellID";
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID];
	if (cell == nil)
	{
		cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellID];
	}
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    if([indexPath section]==0){
        if([indexPath row]==0){
            cell.textLabel.text=@"不知道怎么画地图?我教你!";
       //     cell.imageView.image = [UIImage imageNamed:@"绘制路线图标.png"];
        }else if([indexPath row]==1){
            cell.textLabel.text=@"喜欢我们的应用吗?";
       //     cell.imageView.image = [UIImage imageNamed:@"评分图标.png"]; 
        }else if([indexPath row]==2){
            cell.textLabel.text=@"有什么意见吗?进来说说!";
       //     cell.imageView.image = [UIImage imageNamed:@"意见反馈图标.png"];
        }else if([indexPath row]==3){
            cell.textLabel.text=@"查看新版本!有惊喜!";
        }
    }else if([indexPath section]==1){
        if([indexPath row]==0){
            cell.textLabel.text = @"退出";
       //     cell.imageView.image = [UIImage imageNamed:@"退出图标.png"];
        }
    }
    cell.imageView.frame=CGRectMake(cell.imageView.frame.origin.x, cell.imageView.frame.origin.y, 20, 20);
    cell.textLabel.textColor = [UIColor getColor:@"303030"];
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    return cell;
}

-(void)quitButtonClick{
    SinaApiRequestUtil* requestUtil=[SinaApiRequestUtil getSinglton];
    [requestUtil quit];
     [SFHFKeychainUtils deleteItemForUsername:staticInfo.user.userId andServiceName:@"riddingapp" error:nil];
    staticInfo.user.userId=@"";
    staticInfo.user.accessToken=@"";
    staticInfo.user.authToken=@"";
    staticInfo.logined=false;
    RiddingAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
    [self.navigationController popToViewController:appDelegate.rootViewController animated:YES];
}

- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if([indexPath section]==0){
        if([indexPath row]==0){
            TutorialViewController *tutorialViewController = [[TutorialViewController alloc]initWithNibName:@"TutorialViewController" bundle:nil];
            [self.navigationController pushViewController:tutorialViewController animated:YES];
        }else if([indexPath row]==1){
            NSString *str = [NSString stringWithFormat:  
                             @"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=%d", 557123754];   
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
        }else if([indexPath row]==2){
            [UMFeedback showFeedback:self withAppkey:@"4fa8cecb52701515c0000050"];
            self.navigationController.navigationBarHidden = NO;
        }else if([indexPath row]==3){
            NSString *str = @"itms-apps://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=557123754"; 
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
        }
    }else if([indexPath section]==1){
        if([indexPath row]==0){
            SinaApiRequestUtil* requestUtil=[SinaApiRequestUtil getSinglton];
            [requestUtil quit];
            [self quitButtonClick];
        }
    }
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark -
#pragma mark Table Delegate Methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
           editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return UITableViewCellEditingStyleNone; 
} 
@end
